import time
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.common.by import By
from webdriver_manager.core import driver
from common.Utils import ChatroomDriver

#测试聊天室登录界面
class Login:
    url=""
    driver=""
    def __init__(self):
        self.url="http://127.0.0.1:8080/login.html"
        self.driver=ChatroomDriver.driver
        self.driver.get(self.url)

    # 成功登录的测试用例
    def LoginSucTest(self):

        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #切换到警告窗
        alert = self.driver.switch_to.alert
        #检查是否登录成功
        text = alert.text
        alert.accept()
        assert text == "登录成功!"
        #添加屏幕截图
        ChatroomDriver.getScreeShot()
        #返回到登录界面
        # self.driver.back()

    # 异常登录的测试用例
    def LoginFailTest(self):
        # 密码错误
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        #输入正确的用户名 错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        alert = self.driver.switch_to.alert
        text = alert.text
        assert text == "登录失败!"
        alert.accept()
        ChatroomDriver.getScreeShot()

        # # 用户名错误
        # self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        # self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # # 输入错误的用户名 正确的密码
        # self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("张三")
        # self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        # self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # alert = self.driver.switch_to.alert
        # text = alert.text
        # assert text == "登录失败!"
        # alert.accept()
        # ChatroomDriver.getScreeShot()


        # # 用户名为空
        # self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        # self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("")
        # self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        # self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # alert = self.driver.switch_to.alert
        # text = alert.text
        # assert text == "当前输入的用户名或者密码为空!"
        # alert.accept()

        # # 密码为空
        # self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        # self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        # self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("")
        # self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # alert = self.driver.switch_to.alert
        # text = alert.text
        # assert text == "当前输入的用户名或者密码为空!"
        # alert.accept()
        # self.driver.quit()

# login=Login()
# login.LoginSucTest()
# login.LoginFailTest()


